//
//  HKNavigationController.m
//  BaiSiBuDeJie
//
//  Created by 胡锦涛 on 2018/11/30.
//  Copyright © 2018 胡锦涛. All rights reserved.
//

#import "HKNavigationController.h"

@interface HKNavigationController ()<UIGestureRecognizerDelegate>

@end

@implementation HKNavigationController

+ (void)load
{
    UINavigationBar *navBar = [UINavigationBar appearanceWhenContainedInInstancesOfClasses:@[HKNavigationController.self]];
    // 只要是通过模型设置,都是通过富文本设置
    // 设置导航条标题 => UINavigationBar
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont boldSystemFontOfSize:20];
    [navBar setTitleTextAttributes:attrs];

    // 设置导航条背景图片
    [navBar setBackgroundImage:[UIImage imageNamed:@"navigationbarBackgroundWhite"] forBarMetrics:UIBarMetricsDefault];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 控制手势什么时候触发,只有非根控制器才需要触发手势
    //①设置边缘滑动返回
    //self.interactivePopGestureRecognizer.delegate = self;
    //FIXME:BUG:假死状态:程序还在运行,但是界面死了.(根式退不需要滑动返回)
    
    //②设置全屏滑动返回
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wundeclared-selector"
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self.interactivePopGestureRecognizer.delegate action:@selector(handleNavigationTransition:)];
#pragma clang diagnostic pop
    [self.view addGestureRecognizer:pan];
    // 控制手势什么时候触发,只有非根控制器才需要触发手势
    pan.delegate = self;
    // 禁止之前手势
    self.interactivePopGestureRecognizer.enabled = NO;
    /*
      为什么导航控制器的手势不是全屏滑动返回=>
     */
}
// <_UINavigationInteractiveTransition: 0x7f9c948302a0>:手势代理
/*
 UIPanGestureRecognizer
 UIScreenEdgePanGestureRecognizer:导航滑动手势
 target=<_UINavigationInteractiveTransition 0x7fdc4a740440>)
 action=handleNavigationTransition:
 
 <UIScreenEdgePanGestureRecognizer: 0x7fdc4a740120; state = Possible; delaysTouchesBegan = YES; view = <UILayoutContainerView 0x7fdc4a73e690>; target= <(action=handleNavigationTransition:, >>
 
 */
#pragma mark - UIGestureRecognizerDelegate
// 决定是否触发手势
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
{
    return self.childViewControllers.count > 1;//非根视图控制器才可以触发
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
   
    if (self.childViewControllers.count > 0) { // 非根控制器
        //FIXME:恢复滑动返回功能 -> 分析:把系统的返回按钮覆盖 -> 1.手势失效(1.手势被清空 2.可能手势代理做了一些事情,导致手势失效)
        // 设置返回按钮,只有非根控制器(二级界面)
        viewController.navigationItem.leftBarButtonItem = [UIBarButtonItem backItemWithimage:[UIImage imageNamed:@"navigationButtonReturn"] highImage:[UIImage imageNamed:@"navigationButtonReturnClick"]  target:self action:@selector(back) title:@"返回"];
        // 隐藏tabbar
        viewController.hidesBottomBarWhenPushed = YES;
        NSLog(@"%@",self.interactivePopGestureRecognizer);//UIScreenEdgePanGestureRecognizer
    }
    
    // 真正在跳转
    [super pushViewController:viewController animated:animated];
    
}

- (void)back
{
    [self popViewControllerAnimated:YES];
}

@end
